package demo4.iml;

import demo4.pojo.User;
import demo4.util.DBUtil;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapperImp implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;

    @Override
    public int insert(User user) {
        System.out.println("向数据库插入用户信息");
        try {
            Connection connection = dbUtil.getConnection();
            String sql="insert into user(username,password,nickname,age) "+"values (?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getNickName());
            preparedStatement.setInt(4,user.getAge());
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    @Override
    public User selectByUsername(String username) {
        System.out.println("向数据库查询用户信息");
        try {
            Connection connection = dbUtil.getConnection();
            String sql="select * from user "+"where username=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                User user=new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setNickName(resultSet.getString("nickname"));
                user.setAge(resultSet.getInt("age"));
                return  user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> selectAll() {
        List<User> userList = new ArrayList<>();
        try (Connection connection = dbUtil.getConnection();){
            Statement statement = connection.createStatement();
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickName(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                userList.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }
}
